package main

import (
	"fmt"
	"image"
	"path/filepath"

	"demserver/src/elevation"
)

func main() {
	// Use the actual GeoTIFF file
	filename := "./demdata/ClipResult.tif"

	// Get absolute path to the file
	absPath, err := filepath.Abs(filename)
	if err != nil {
		fmt.Printf("Failed to get absolute path: %v\n", err)
		return
	}

	fmt.Printf("Loading GeoTIFF file: %s\n", absPath)
	geoTiff, err := elevation.LoadGeoTIFF(absPath)
	if err != nil {
		fmt.Printf("Failed to load GeoTIFF: %v\n", err)
		return
	}
	fmt.Println("GeoTIFF loaded successfully")

	// Analyze the relationship between pixel values and expected elevations
	testData := map[string][3]float64{
		"103.54607363636364-31.02659757575758": {31.02659757575758, 103.54607363636364, 780.2126995110525},
		"103.572044-31.037039":                 {31.037039, 103.572044, 806.0841527110774},
		"103.5912788-31.0304264":               {31.0304264, 103.5912788, 773.8375500820072},
		"103.607449-31.0013281":                {31.0013281, 103.607449, 727.5249991936447},
		"103.61049-30.99902":                   {30.99902, 103.61049, 731.2953275545822},
		"104.18999-31.09466":                   {31.09466, 104.18999, 506.7021200994833},
		"103.64307-30.66861":                   {30.66861, 103.64307, 551.6861936104318},
		"104.04-30.67":                         {30.67, 104.04, 493.5921921968492},
		"103.78443-30.12122":                   {30.12122, 103.78443, 422.80135518608773},
		"104.24654-29.93688":                   {29.93688, 104.24654, 372.7077051609116},
	}

	fmt.Println("Analyzing relationship between pixel values and expected elevations:")
	fmt.Printf("%-40s %-15s %-15s %-15s %-15s\n", "Coordinate", "Lat", "Lon", "Expected", "Pixel Value")

	var sumRatio float64
	var count int

	for key, data := range testData {
		lat, lon, expected := data[0], data[1], data[2]

		x, y, err := geoTiff.LatLonToPixel(lat, lon)
		if err != nil {
			fmt.Printf("Failed to convert coordinates (%f, %f): %v\n", lat, lon, err)
			continue
		}

		// Check if coordinates are within bounds
		if x < 0 || y < 0 || x >= float64(geoTiff.Width) || y >= float64(geoTiff.Height) {
			fmt.Printf("%-40s %-15.6f %-15.6f %-15.6f %-15s\n", key, lat, lon, expected, "Out of bounds")
			continue
		}

		// Get the raw pixel value directly
		xInt := int(x)
		yInt := int(y)
		pixelValue := getRawPixelValue(geoTiff, xInt, yInt)

		fmt.Printf("%-40s %-15.6f %-15.6f %-15.6f %-15.0f\n", key, lat, lon, expected, pixelValue)

		// Calculate ratio
		if pixelValue != 0 {
			ratio := expected / pixelValue
			sumRatio += ratio
			count++
			fmt.Printf("  Ratio (expected/pixel): %f\n", ratio)
		}
	}

	if count > 0 {
		averageRatio := sumRatio / float64(count)
		fmt.Printf("\nAverage ratio (expected/pixel): %f\n", averageRatio)
		fmt.Printf("This suggests the formula: elevation = pixel_value * %f\n", averageRatio)
	}
}

func getRawPixelValue(geoTiff *elevation.GeoTIFF, x, y int) float64 {
	// Get the raw pixel value without any interpolation
	grayImg, ok := geoTiff.Image.(*image.Gray16)
	if ok {
		value := grayImg.Gray16At(x, y)
		return float64(value.Y)
	}
	return 0
}
